package service

import (
	"BlazeRider-backend/app/manager"
	"BlazeRider-backend/app/models"
	"BlazeRider-backend/app/utils"
	"BlazeRider-backend/config"
	"fmt"
)

type AuthService struct {
}

func (as AuthService) SetAuth(liveRoomToken string) (*models.LiveRoomUserInfo, string, error) {
	liveRoomUserInfo, err := DouYinService{}.RequestDouYinForLiveLogin(liveRoomToken)
	if err != nil {
		return nil, "", err
	}

	_, err = manager.Get().LiveRoomManager().CreateLiveRoomIfNotExist(liveRoomUserInfo.RoomId, liveRoomUserInfo.AnchorOpenId, liveRoomUserInfo.AvatarUrl, liveRoomUserInfo.NickName)
	if err != nil {
		return nil, "", err
	}

	liveLoginToken, err := utils.GenToken(config.Get().App.Name, 24, liveRoomUserInfo)
	if err != nil {
		return nil, "", err
	}
	return &liveRoomUserInfo, liveLoginToken, nil
}

func (as AuthService) CheckAuth(liveRoomLoginToken string) (*utils.MyClaims, error) {
	claims, err := utils.ParseToken(liveRoomLoginToken)
	if err != nil {
		return nil, fmt.Errorf("invalid token, error : %v", err)
	}
	return claims, nil
}
